<?php

namespace SDXApi\Adapters;


/**
 * 首页模版适配器
 * Class HomeTemplateAdapter
 * @package SDXApi\Service
 */
class HomeTemplateAdapter
{
	/**
	 * @var
	 */
	protected $app;

	/**
	 * 实例名称
	 * @var array
	 */
	protected $instance = array(
		1 	=> 'SDXApi\Service\HomeTemplateFontService',
		2 	=> 'SDXApi\Service\HomeTemplateImageService',
		3 	=> 'SDXApi\Service\HomeTemplateImageService',
		4 	=> 'SDXApi\Service\HomeTemplateGoodsService',
		5 	=> 'SDXApi\Service\HomeTemplateSeminarService',
		6 	=> 'SDXApi\Service\HomeTemplateBuyNowService',
		7 	=> 'SDXApi\Service\HomeTemplateRecommendService',
		8 	=> 'SDXApi\Service\HomeTemplateCategoryService',
		9 	=> 'SDXApi\Service\HomeTemplateSearchAddressService',
		10	=> 'SDXApi\Service\HomeTemplateTransverseService',
		11	=> 'SDXApi\Service\HomeTemplateAlbumService',
        12	=> 'SDXApi\Service\HomeTemplateImageGroupService',
        13  => 'SDXApi\Service\HomeTemplateVogueService',
        14  => 'SDXApi\Service\HomeTemplateBuyTakeService',
        15  =>  'SDXApi\Service\HomeTemplateGoodsNewService',
        16 	=> 'SDXApi\Service\HomeTemplateImageService',
        17  => 'SDXApi\Service\HomeTemplateImageBackgroundService',
	);

	/**
	 * @var array
	 */
	protected $error = array();

	/**
	 * 构造实例
	 * HomeTemplateAdapter constructor.
	 * @param int $type
	 */
	public function __construct($type = 0)
	{
		$instanceName = $this->instance[$type];

		if (class_exists($instanceName)) {
			$this->app = new $instanceName();
		}
		else{
			E('获取数据出错', 5001);
		}

		return $this;
	}

	/**
	 * 处理模块
	 * Created by Jerry
	 * @param array $stencil
	 * @param int $userId
	 * @param int $wid
	 * @return bool
	 */
	public function handle($stencil = array(), $userId = 0, $wid = 8)
	{
		if (get_class($this->app)) {
			return $this->app->handle($stencil, $userId, $wid);
		}

		return false;
	}


	/**
	 * 获取错误信息
	 * Created by Jerry
	 * @return string
	 */
	public function getErrorMessage()
	{
		return implode(',', $this->error);
	}
}